package com.haitaoit.chengquan.module.home.activity.maintenance;

import android.app.Service;
import android.os.Bundle;
import android.os.Vibrator;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import com.haitaoit.chengquan.R;
import com.haitaoit.chengquan.baiduservice.LocationService;
import com.haitaoit.chengquan.base.BaseActivity;
import com.haitaoit.chengquan.utils.ToastUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

public class MapGpsActivity extends BaseActivity {
    @ViewInject(R.id.bmapView)
    MapView mMapView;


    private double latitude = 39.915174;
    private double longitude = 121.446496;
    private boolean isToast = true;
    private BaiduMap mBaiduMap;
    private LocationService locationService;
    private BDLocationListener mListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SDKInitializer.initialize(getApplicationContext());
        setContentView_V(R.layout.activity_map_gps);
        setTitle_V("地址定位");
        setLeftShow(1, R.mipmap.left);
        backLeft_V();
        ViewUtils.inject(this);


        mBaiduMap = mMapView.getMap();
        mListener = new BDLocationListener() {
            @Override
            public void onReceiveLocation(BDLocation location) {
                if (isToast) {
                    isToast = false;
                    if (null != location && location.getLocType() != BDLocation.TypeServerError) {
                        LatLng ll = new LatLng(location.getLatitude(), location.getLongitude());
                        MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll);
                        MapStatusUpdate u1 = MapStatusUpdateFactory.newMapStatus(new MapStatus.Builder().zoom(13).build());
                        mBaiduMap.setMapStatus(u);
                        mBaiduMap.setMapStatus(u1);
                    }
                    if (!("").equals(location.getCity())) {
                        if (location.getLocType() == BDLocation.TypeGpsLocation) {// GPS定位结果
                            ToastUtils.showToast("定位成功");
                        } else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {// 网络定位结果
                            ToastUtils.showToast("网络定位成功");
                        } else if (location.getLocType() == BDLocation.TypeOffLineLocation) {// 离线定位结果
                            ToastUtils.showToast("定位成功");
                        } else if (location.getLocType() == BDLocation.TypeServerError) {
                            ToastUtils.showToast("定位失败,无法搜索到商家");
                        } else if (location.getLocType() == BDLocation.TypeNetWorkException) {
                            ToastUtils.showToast("定位失败,无法搜索到商家,请检查网络是否通畅");
                        } else if (location.getLocType() == BDLocation.TypeCriteriaException) {
                            ToastUtils.showToast("定位失败,无法搜索到商家");
                        }
                        ToastUtils.showToast("定位城市:" + location.getCity());

                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                        LatLng point = new LatLng(latitude, longitude);
                        // 构建Marker图标
                        BitmapDescriptor bitmap = BitmapDescriptorFactory.fromResource(R.mipmap.pic_tra);
                        // 构建MarkerOption，用于在地图上添加Marker
                        OverlayOptions option = new MarkerOptions().position(point).icon(bitmap);
                        // 在地图上添加Marker，并显示
                        mBaiduMap.addOverlay(option);
                    }
                }
            }

        };
        locationService = new LocationService(this);
        locationService.registerListener(mListener);
        Vibrator mVibrator = (Vibrator) this.getSystemService(Service.VIBRATOR_SERVICE);
        locationService.start();// 定位SDK

        //121.506258,31.245434
        LatLng point = new LatLng(Double.valueOf(getIntent().getStringExtra("latitude")),
                Double.valueOf(getIntent().getStringExtra("longitude")));
        // 构建Marker图标
        BitmapDescriptor bitmap = BitmapDescriptorFactory.fromResource(R.mipmap.pic42);
        // 构建MarkerOption，用于在地图上添加Marker
        OverlayOptions option = new MarkerOptions().position(point).icon(bitmap);
        // 在地图上添加Marker，并显示
        mBaiduMap.addOverlay(option);
    }

    @Override
    public void onResume() {
        locationService.start();// 定位SDK
        super.onResume();
    }

    @Override
    public void onPause() {
        locationService.stop();
        super.onPause();
    }
}
